package cn.usian.pricingstrategy.driver.service.impl;

import cn.usian.pojo.Result;
import cn.usian.pojo.pricingstrategy.driver.OperateDriverCharges;
import cn.usian.pricingstrategy.driver.mapper.DriverMapper;
import cn.usian.pricingstrategy.driver.service.IDriverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class DriverServiceImpl implements IDriverService {
    @Autowired
    private DriverMapper driverMapper;

    private OperateDriverCharges validation(OperateDriverCharges charges){
        if(charges.getServiceType() == 1){
            charges.setServicePrice(BigDecimal.valueOf(0));
        }
        if(charges.getServiceType() == 2){
            charges.setServiceProportion((double) 0);
        }
        if(charges.getManageType() == 1){
            charges.setManagePrice(BigDecimal.valueOf(0));
        }
        if(charges.getManageType() == 2){
            charges.setManageProportion((double) 0);
        }
        return charges;
    }

    @Override
    public Result save(OperateDriverCharges operateDriverCharges) {
        log.info("前端传入数据：{}" , operateDriverCharges);
        Optional<OperateDriverCharges> charges = Optional.ofNullable(operateDriverCharges);
        if (charges.isPresent()) {
            OperateDriverCharges validation = validation(operateDriverCharges);
            log.info("修改数据：{}" , validation);
            driverMapper.update(validation);
        }else{
            return Result.error("修改失败");
        }
        return Result.ok("修改成功");
    }

    @Override
    public OperateDriverCharges findById(long id) {
        return driverMapper.findById(id);
    }

    @Override
    public List<OperateDriverCharges> list() {
        return driverMapper.list();
    }
}
